/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package service.posServices;

import com.openbrew.openbrewrf.POSProductServingType;
import com.openbrew.openbrewrf.POSProducts;
import com.openbrew.openbrewrf.POSSetup;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import service.AbstractFacade;

/**
 *
 * @author chapmand
 */
@Stateless
@Path("com.openbrew.openbrewrf.posproductservingtype")
public class POSProductServingTypeFacadeREST extends AbstractFacade<POSProductServingType> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;

    public POSProductServingTypeFacadeREST() {
        super(POSProductServingType.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public POSProductServingType create(@WebParam(name = "posproductservingtype") POSProductServingType entity) {
        entity = super.create(entity);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(POSProductServingType entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
        em.getEntityManagerFactory().getCache().evictAll();
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public POSProductServingType find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<POSProductServingType> findAll() {
        return super.findAll();
    }

    @GET
    @Path("getAllCurrentTasters")
    @Consumes({"application/xml"})
    public List<POSProductServingType> findAllCurrentTasters() {
        //Load Setup to get Glassware id for tasters.
        TypedQuery<POSSetup> setupQuery = em.createQuery(
                "SELECT s FROM POSSetup s WHERE s.id = '1'", POSSetup.class);
        POSSetup setup = setupQuery.getSingleResult();
        if(setup.getGlassware()!=null){
            //Get all PSTs that have set glassware.
            TypedQuery<POSProductServingType> pstQuery = em.createQuery(
                    "SELECT p FROM POSProductServingType p WHERE p.glassware.id = ?1"
                    , POSProductServingType.class);
            pstQuery.setParameter(1, setup.getGlassware().getId());
            List<POSProductServingType> psts = pstQuery.getResultList();
            //Get all Products that are on tap.
            TypedQuery<POSProducts> productQuery = em.createQuery(
                    "SELECT p FROM POSProducts p WHERE p.productOnTap = 'true'"
                    , POSProducts.class);
            List<POSProducts> products = productQuery.getResultList();
            //Create final list with only items that are on tap.
            List<POSProductServingType> tasters = new ArrayList<POSProductServingType>();
            for(final POSProducts product : products){
                if(product.getProductBeer()!=null){
                    for(final POSProductServingType pst : psts){
                        if(pst.getServingTypeBeer()!=null){
                            if(pst.getServingTypeBeer().getbId() == product.getProductBeer().getbId()){
                                tasters.add(pst);
                            }
                        }
                    }
                } else {
                    for(final POSProductServingType pst : psts){
                        if(pst.getServingTypeBeer() == null){
                            tasters.add(pst);
                        }
                    }
                }
            }
            return tasters;
        }
        return null;
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<POSProductServingType> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
